package org.jeecg.modules.demo.temple.donation.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.temple.activity.entity.TempleActivity;
import org.jeecg.modules.demo.temple.donation.entity.TempleDonation;
import org.jeecg.modules.demo.temple.donation.entity.vo.TempleDonationQuery;
import org.jeecg.modules.demo.temple.donation.service.ITempleDonationService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.temple.order.entity.TempleDonationOrder;
import org.jeecg.modules.demo.temple.order.service.ITempleDonationOrderService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 捐赠项目
 * @Author: jeecg-boot
 * @Date:   2022-04-24
 * @Version: V1.0
 */
@Api(tags="捐赠项目")
@RestController
@RequestMapping("/temple.donation/templeDonation")
@Slf4j
public class TempleDonationController extends JeecgController<TempleDonation, ITempleDonationService> {
	@Autowired
	private ITempleDonationService templeDonationService;

	@Autowired
	private ITempleDonationOrderService templeDonationOrderService;

	/**
	 *
	 * @Description 分页列表条件查询
	 * @date 2022/6/7 10:27
	 * @param templeDonationQuery
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return Result<IPage<TempleDonation>>
	 */


	 @ApiOperation(value="捐赠项目-分页列表条件查询", notes="捐赠项目-分页列表条件查询")
	 @GetMapping({"/donationQueryPageList","/api/donationQueryPageList"})
	 public Result<IPage<TempleDonation>> donationQueryPageList(TempleDonationQuery templeDonationQuery,
																@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																@RequestParam(name="pageSize", defaultValue="5") Integer pageSize,
																HttpServletRequest req) {
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 if(user!=null){
			 templeDonationQuery.setSysOrgCode(user.getOrgCode());
		 }
		 QueryWrapper<TempleDonation> queryWrapper = new QueryWrapper<>() ;
		 // 分页条件
		 Page<TempleDonation> page = new Page<TempleDonation>(pageNo, pageSize);
		 String name = templeDonationQuery.getName();
		 String begin = templeDonationQuery.getBegin();
		 String end = templeDonationQuery.getEnd();
		 String sysOrgCode= templeDonationQuery.getSysOrgCode() ;
		 //判断条件是否为空，拼接条件
		 if (!StringUtils.isEmpty(name)) {
		 	queryWrapper.like("name", name);
		 }
		 if (!StringUtils.isEmpty(begin)) {
			 queryWrapper.ge("begin_date", begin);
		 }
		 if (!StringUtils.isEmpty(end)) {
			 queryWrapper.le("begin_date", end);
		 }
		 if (!StringUtils.isEmpty(sysOrgCode)) {
			 queryWrapper.eq("sys_org_code", sysOrgCode);
		 }
		 queryWrapper.orderByDesc("begin_date");
		 // 查询
		 IPage<TempleDonation> pageList = templeDonationService.page(page, queryWrapper);
		 //QueryWrapper<TempleDonation> queryWrapper = QueryGenerator.initQueryWrapper(templeDonation, req.getParameterMap());
		 for(TempleDonation t : pageList.getRecords()){
			 TempleDonationOrder order = new TempleDonationOrder();
			 order.setDonationId(t.getId());
			 QueryWrapper<TempleDonationOrder> wrapper = QueryGenerator.initQueryWrapper(order, req.getParameterMap());
			 int cnt = templeDonationOrderService.count(wrapper);
			 t.setDonationCnt(cnt);
		 }
		 return Result.OK(pageList);
	 }




	/**
	 * 分页列表查询
	 *
	 * @param templeDonation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "捐赠项目-分页列表查询")
	@ApiOperation(value="捐赠项目-分页列表查询", notes="捐赠项目-分页列表查询")
	@GetMapping({"/list","/api/list"})
	public Result<IPage<TempleDonation>> queryPageList(TempleDonation templeDonation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="5") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TempleDonation> queryWrapper = QueryGenerator.initQueryWrapper(templeDonation, req.getParameterMap());
		queryWrapper.orderByDesc("create_time");
		Page<TempleDonation> page = new Page<TempleDonation>(pageNo, pageSize);
		IPage<TempleDonation> pageList = templeDonationService.page(page, queryWrapper);
		for(TempleDonation t : pageList.getRecords()){
			TempleDonationOrder order = new TempleDonationOrder();
			order.setDonationId(t.getId());
			order.setIsPay("Y");
			QueryWrapper<TempleDonationOrder> wrapper = QueryGenerator.initQueryWrapper(order, req.getParameterMap());
			int cnt = templeDonationOrderService.count(wrapper);
			t.setDonationCnt(cnt);
		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param templeDonation
	 * @return
	 */
	@AutoLog(value = "捐赠项目-添加")
	@ApiOperation(value="捐赠项目-添加", notes="捐赠项目-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TempleDonation templeDonation) {
		templeDonationService.save(templeDonation);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param templeDonation
	 * @return
	 */
	@AutoLog(value = "捐赠项目-编辑")
	@ApiOperation(value="捐赠项目-编辑", notes="捐赠项目-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TempleDonation templeDonation) {
		templeDonationService.updateById(templeDonation);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "捐赠项目-通过id删除")
	@ApiOperation(value="捐赠项目-通过id删除", notes="捐赠项目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		templeDonationService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "捐赠项目-批量删除")
	@ApiOperation(value="捐赠项目-批量删除", notes="捐赠项目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.templeDonationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "捐赠项目-通过id查询")
	@ApiOperation(value="捐赠项目-通过id查询", notes="捐赠项目-通过id查询")
	@GetMapping({"/queryById","/api/queryById"})
	public Result<TempleDonation> queryById(@RequestParam(name="id",required=true) String id) {
		TempleDonation templeDonation = templeDonationService.getById(id);
		if(templeDonation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(templeDonation);
	}
	/**
	 *
	 * @Description 查询项目人数和金额
	 * @date 2022/5/17 8:44
	 * @param id
	 * @return Result
	 */
	 @ApiOperation(value="捐赠项目-通过id查询人数，金额", notes="捐赠项目-通过id查询人数，金额")
	 @GetMapping({"/queryByIdTotal","/api/queryByIdTotal"})
	 public Result queryByIdTotal(@RequestParam(name="id",required=true) String id) {
		 Map total = templeDonationOrderService.totalNumMoney(id);

		 if(total.size()==0) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(total);
	 }


    /**
    * 导出excel
    *
    * @param request
    * @param templeDonation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TempleDonation templeDonation) {
        return super.exportXls(request, templeDonation, TempleDonation.class, "捐赠项目");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TempleDonation.class);
    }

}
